<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\middleware\api\Auth;
use app\model\RecordIntegral;
use app\model\Users;
use app\model\UsersIntegral;
use dh2y\qrcode\QRcode;
use OSS\OssClient;
use think\facade\Filesystem;
use think\Request;

class User extends BaseController
{
    protected $middleware=[Auth::class];

    /**
     * 用户详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
       $model= Users::with('grade')->find(request()->userid)->hidden(['password']);

       return $this->success($model);
    }

    /**
     * 修改用户资料
     * @return \think\response\Json
     */
    public function edit()
    {
        $params=\request()->param();
        if(!empty($params['password'])){
            $params['password']=password_hash($params['password'],PASSWORD_DEFAULT);
        }
        Users::where('id',request()->userid)->update($params);
        return $this->success();
    }


    /**
     * 注销用户
     * @return \think\response\Json
     */
    public function delete()
    {
        Users::where('id',request()->userid)->delete();
        return $this->success();
    }

    /**
     * 积分记录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function record()
    {
      return $this->success(  RecordIntegral::where('users_id',request()->userid)->select());
    }
}
